package tree.leetcode_199;

import java.util.ArrayList;
import java.util.List;

public class DFS {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        dfs(root, 0, res);
        return res;
    }
    public void dfs(TreeNode root, int depth, List<Integer> res) {
        if (root == null) {
            return;
        }
        if (depth == res.size()) {
            res.add(root.val);
        }
        // 先遍历右子树，每次都是最右边的节点
        dfs(root.right, depth + 1, res);
        dfs(root.left, depth + 1, res);
    }
}
